package com.yanqu.road.dao.impl.wingroom;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.UserWingRoomPlan;
import com.yanqu.road.entity.wingroom.WingRoomRegion;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/3/8 17:55
 */
public class UserWingRoomPlanDaoImpl extends TempDataObjectDao<UserWingRoomPlan> {
    public static String insertSql = "INSERT INTO t_u_wingroom_plan(userId,planUid,planName,regionMap,isDelete) values(?,?,?,?,?)";
    @Override
    protected boolean addData(UserWingRoomPlan data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlanUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getPlanName()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRegionMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDelete()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wingroom_plan set planName=?, regionMap=?, isDelete=? where userId=? and planUid = ?";

    @Override
    protected boolean updateData(UserWingRoomPlan data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, data.getPlanName()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRegionMap())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDelete()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlanUid()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWingRoomPlan parseData(ResultSet rs) throws SQLException {
        UserWingRoomPlan data = new UserWingRoomPlan();
        data.setUserId(rs.getLong("userId"));
        data.setPlanUid(rs.getLong("planUid"));
        data.setPlanName(rs.getString("planName"));
        data.setRegionMap(JSONObject.parseObject(rs.getString("regionMap"), new TypeReference<Map<Integer, WingRoomRegion>>() {
        }));
        data.setDelete(rs.getBoolean("isDelete"));
        return data;
    }

    public static String selectSql = "SELECT * FROM t_u_wingroom_plan WHERE userId = ? and isDelete = 0";

    public Map<Long, UserWingRoomPlan> getUserWingRoomPlanMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserWingRoomPlan> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWingRoomPlan data = parse(rs);
                dataMap.put(data.getPlanUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String selectPlanSql = "SELECT * FROM t_u_wingroom_plan WHERE userId = ? and planUid = ? and isDelete = 0";

    public UserWingRoomPlan getUserWingRoomPlan(long userId, long planUid) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectPlanSql);
            call.setLong(1, userId);
            call.setLong(2, planUid);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getMaxPlanIdSql = "SELECT IFNULL(MAX(planUid),0) AS maxId FROM t_u_wingroom_plan WHERE userId = ?";
    public long getPlanMaxId(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getMaxPlanIdSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }
}
